package org.jeecgframework.core.common.model.json;

import java.util.List;

import org.jeecgframework.tag.vo.datatable.SortDirection;


/**
 * easyui的datagrid向后台传递参数使用的model
 * 
 * @author
 * 
 */
public class DataGrid {

	private int page = 1;// 当前页
	private int rows = 10;// 每页显示记录数
	private String sort = null;// 排序字段名
	private SortDirection order = SortDirection.asc;// 按什么排序(asc,desc)
	private String field;//字段
	private String treefield;//树形数据表文本字段
	private List reaults;// 结果集
	private int total;//总记录数
	private String footer;//合计列
	private int lastpage;		//最后一页
	
	String getActiveStr(int page , int now){
		if(page == now) return " class='active' ";
		return "";
	}
	
	public String getPagehtml(){
		StringBuffer buffer = new StringBuffer("");
		if(getLastpage()<=5){
			for(int i=1;i<=getLastpage()&&i<=5;i++){
				buffer.append("<a href='#' "+getActiveStr(page, i)+" onclick='setPage("+i+")'>"+i+"</a>");
			}
			return buffer.toString();
		}
		
		int midden_last = 1;
		int midden_frist = 1;
		
		
		buffer.append("<a href='#' "+getActiveStr(page, 1)+" onclick='setPage(1)'>1</a>");		//添加第一页
		
		//获取选中页的前后
		if(page <= 2){
			midden_last = 4;
			midden_frist = 2;
			for(int i=2;i<=getLastpage()&&i<=4;i++){
				buffer.append("<a href='#' "+getActiveStr(page, i)+" onclick='setPage("+i+")'>"+i+"</a>");
			}
		}
		
		if(page > 2 && page<getLastpage()-2){
			for(int i=page-1;i<=page+1;i++){
				buffer.append("<a href='#' "+getActiveStr(page, i)+" onclick='setPage("+i+")'>"+i+"</a>");
			}
		}
		
		if(page+2>=getLastpage()){
			midden_frist = getLastpage()-2;
			midden_last = getLastpage();
			if(midden_frist>2){
				buffer.append("...");
			}
			for(int i=getLastpage()-2;i<=getLastpage();i++){
				buffer.append("<a href='#' "+getActiveStr(page, i)+" onclick='setPage("+i+")'>"+i+"</a>");
			}
		}
		
		if(midden_last<getLastpage())
			buffer.append("...");
		
		
		if(getLastpage()>1 && page<getLastpage()-2){
			buffer.append("<a href='#' onclick='setPage("+getLastpage()+")'>"+getLastpage()+"</a>");
		}
		//添加最后一页
		return buffer.toString();
	}
	
	public int getLastpage() {
		if(total == 0) return 1;
		if(total%rows == 0) return total/rows;
		return total/rows+1;
	}

	public void setLastpage(int lastpage) {
		this.lastpage = lastpage;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getField() {
		return field;
	}

	public List getReaults() {
		return reaults;
	}

	public void setReaults(List reaults) {
		this.reaults = reaults;
	}

	public void setField(String field) {
		this.field = field;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public SortDirection getOrder() {
		return order;
	}

	public void setOrder(SortDirection order) {
		this.order = order;
	}
	public String getTreefield() {
		return treefield;
	}

	public void setTreefield(String treefield) {
		this.treefield = treefield;
	}

	public String getFooter() {
		return footer;
	}

	public void setFooter(String footer) {
		this.footer = footer;
	}
	
}
